#!/bin/bash

# ============================================
# 重新创建 MySQL 容器脚本（使用 3307 端口）
# ============================================

echo "=========================================="
echo "重新创建 MySQL 容器（端口 3307）"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 1. 停止并删除现有容器
echo -e "${YELLOW}步骤 1: 停止并删除现有容器...${NC}"
if docker ps -a | grep -q thunderbit-mysql; then
    echo "发现现有容器，正在删除..."
    docker stop thunderbit-mysql 2>/dev/null
    docker rm thunderbit-mysql 2>/dev/null
    echo -e "${GREEN}✓ 现有容器已删除${NC}"
else
    echo "未发现现有容器"
fi
echo ""

# 2. 创建新容器（映射到 3307 端口）
echo -e "${YELLOW}步骤 2: 创建新容器（映射到 3307 端口）...${NC}"
docker run -d \
  --name thunderbit-mysql \
  -e MYSQL_ROOT_PASSWORD=rootpassword \
  -e MYSQL_USER=thunderbit \
  -e MYSQL_PASSWORD=thunderbit123 \
  -p 3307:3306 \
  -v thunderbit-mysql-data:/var/lib/mysql \
  --restart unless-stopped \
  mysql:8

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 容器创建成功${NC}"
else
    echo -e "${RED}✗ 容器创建失败${NC}"
    exit 1
fi
echo ""

# 3. 等待 MySQL 启动
echo -e "${YELLOW}步骤 3: 等待 MySQL 启动（约 30 秒）...${NC}"
echo -n "等待中"
for i in {1..30}; do
    echo -n "."
    sleep 1
done
echo ""
echo -e "${GREEN}✓ 等待完成${NC}"
echo ""

# 4. 验证容器状态
echo -e "${YELLOW}步骤 4: 验证容器状态...${NC}"
docker ps | grep thunderbit-mysql
echo ""

# 5. 初始化数据库
echo -e "${YELLOW}步骤 5: 初始化数据库...${NC}"
if [ -f "init-database.sql" ]; then
    docker exec -i thunderbit-mysql mysql -u root -prootpassword < init-database.sql
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ 数据库初始化成功${NC}"
    else
        echo -e "${RED}✗ 数据库初始化失败${NC}"
        exit 1
    fi
else
    echo -e "${RED}✗ 找不到 init-database.sql 文件${NC}"
    echo "请确保在 mall 目录下运行此脚本"
    exit 1
fi
echo ""

# 6. 验证数据库
echo -e "${YELLOW}步骤 6: 验证数据库创建...${NC}"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 -e "SHOW DATABASES;"
echo ""

# 7. 插入测试数据
echo -e "${YELLOW}步骤 7: 插入测试数据...${NC}"
docker exec -i thunderbit-mysql mysql -u thunderbit -pthunderbit123 inventory_db <<EOF
INSERT INTO t_product_sku (sku_code, sku_name, product_id, price, stock_quantity, reserved_quantity, status, created_at, updated_at)
VALUES 
('SKU001', 'iPhone 15 Pro 256GB 深空黑色', 1, 7999.00, 100, 0, 'ACTIVE', NOW(), NOW()),
('SKU002', 'MacBook Pro 14寸 M3 芯片', 2, 14999.00, 50, 0, 'ACTIVE', NOW(), NOW()),
('SKU003', 'AirPods Pro 2 主动降噪', 3, 1899.00, 200, 0, 'ACTIVE', NOW(), NOW()),
('SKU004', 'iPad Air 11寸 256GB', 4, 5299.00, 80, 0, 'ACTIVE', NOW(), NOW()),
('SKU005', 'Apple Watch Series 9 GPS', 5, 3199.00, 150, 0, 'ACTIVE', NOW(), NOW())
ON DUPLICATE KEY UPDATE 
    sku_name = VALUES(sku_name),
    price = VALUES(price),
    stock_quantity = VALUES(stock_quantity),
    updated_at = NOW();
EOF

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 测试数据插入成功${NC}"
else
    echo -e "${RED}✗ 测试数据插入失败${NC}"
fi
echo ""

# 8. 验证测试数据
echo -e "${YELLOW}步骤 8: 验证测试数据...${NC}"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 inventory_db -e "SELECT * FROM t_product_sku;"
echo ""

# 9. 显示连接信息
echo -e "${GREEN}=========================================="
echo "✓ MySQL 容器配置完成！"
echo "==========================================${NC}"
echo ""
echo "连接信息:"
echo "  主机: localhost"
echo "  端口: 3307"
echo "  用户: thunderbit"
echo "  密码: thunderbit123"
echo ""
echo "数据库:"
echo "  - order_db"
echo "  - inventory_db"
echo "  - payment_db"
echo ""
echo "测试连接:"
echo "  mysql -h localhost -P 3307 -u thunderbit -pthunderbit123"
echo ""
echo "下一步:"
echo "  1. 启动 Kafka: docker start kafka"
echo "  2. 编译项目: mvn clean package -DskipTests"
echo "  3. 启动服务: cd order-service && mvn spring-boot:run"
echo ""

